"""
点赞模型
映射数据库likes表
"""
from sqlalchemy import Column, Integer, DateTime, ForeignKey, Index
from sqlalchemy.orm import relationship

from app.models.base import BaseModel


class Like(BaseModel):
    """
    点赞模型类
    对应数据库中的likes表
    """
    
    __tablename__ = "likes"
    
    id = Column(
        Integer, 
        primary_key=True, 
        autoincrement=True,
        comment='点赞ID'
    )
    
    user_id = Column(
        Integer, 
        ForeignKey('users.id', ondelete='CASCADE', onupdate='CASCADE'), 
        nullable=False,
        comment='用户ID'
    )
    
    video_id = Column(
        Integer, 
        ForeignKey('videos_detail.id', ondelete='CASCADE', onupdate='CASCADE'), 
        nullable=False,
        comment='视频ID'
    )
    
    # 关系
    user = relationship('User')
    video = relationship('Video', primaryjoin="Like.video_id==Video.id")
    
    # 索引
    __table_args__ = (
        # 用户-视频唯一索引，确保一个用户对一个视频只能点赞一次
        Index('idx_user_video_unique', 'user_id', 'video_id', unique=True),
        # 单列索引优化查询性能
        Index('idx_user_id', 'user_id'),
        Index('idx_video_id', 'video_id'),
    )